//
//  Rank.cpp
//  dot
//
//  Created by zz on 2019/6/18.
//

#include "Rank.hpp"


Scene* Rank::createScene(){
    
    auto rank=Rank::create();
    auto scene=Scene::create();
    scene->addChild(rank);
    return scene;
}
bool Rank::init(){
    
    if(!Layer::init()){
        
        return false;
    }
    
    auto size = Director::getInstance()->getVisibleSize();//获取屏幕大小
    auto bg = Sprite::create("background.png");//添加背景图片
    bg->setScale(size.width/bg->getContentSize().width,size.height/bg->getContentSize().height);
    bg->setPosition(size/2);
    this->addChild(bg);
    
    
    auto icon = Sprite::create("rang.png");//a排行徽章
    icon->setPosition(Vec2(size.width/2,size.height/2+200));
    icon->setColor(Color3B(255,219, 0));//设置图片颜色：金色
    this->addChild(icon);
    
    
    //创建 排行 123，并分别设置tag，到时候更改数组的时候可以根据tag
    //获取到对应的label对象。
    for(int i=0;i<=2;i++){
        
        auto str = __String::createWithFormat("%i.   000",i+1)->getCString();
        auto r1 = Label::create(str," STCaiyun",40);
        r1->setTag(100+i);
        r1->setPosition(Vec2(size.width/2,size.height/2+50-i*80));
        this->addChild(r1);
    }
    
    //返回按钮
    auto back = MenuItemImage::create("home.png","home_ac.png",CC_CALLBACK_0(Rank::back,this));
    back->setScale(0.3);
    back->setPosition(Vec2(30,size.height-30));
    
    auto menu = Menu::create(back,NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu);
    
    this->getSorce();


    return true;
}
void Rank::back(){
    
    Director::getInstance()->replaceScene(HelloWorld::createScene());
}

void  Rank::getSorce(){
    
    auto rm = RankManager::getInstance();
    int v1 = rm->getR("r1");
    int v2 = rm->getR("r2");
    int v3 = rm->getR("r3");
    
    ((Label*)this->getChildByTag(100))->setString(__String::createWithFormat("%i",v1)->getCString());
      ((Label*)this->getChildByTag(101))->setString(__String::createWithFormat("%i",v2)->getCString());
      ((Label*)this->getChildByTag(102))->setString(__String::createWithFormat("%i",v3)->getCString());
    
    
}

